package uk.ac.lkl.takteen.energyapp.data;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class EnergyAppProvider extends ContentProvider {

	private EnergyAppDatabase ead;
	private static final String AUTHORITY = "uk.ac.lkl.takteen.energyapp.data.EnergyAppProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/");

	public static final Uri USER_CHALLENGES_DIR = Uri.parse(CONTENT_URI
			+ EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME + "/");
	public static final Uri RECEIVED_CHALLENGES_DIR = Uri.parse(CONTENT_URI
			+ EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME + "/");
	public static final Uri USER_PLEDGES_DIR = Uri.parse(CONTENT_URI
			+ EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME + "/");
	public static final Uri SHARED_PLEDGES_DIR = Uri.parse(CONTENT_URI
			+ EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME + "/");

	// incoming uri patterns - only for single challenges/pledges
	private static final int USER_CHALLENGE_DIR_URI_INDICATOR = 1;
	private static final int USER_CHALLENGE_ITEM_URI_INDICATOR = 2;
	private static final int RECEIVED_CHALLENGE_DIR_URI_INDICATOR = 3;
	private static final int RECEIVED_CHALLENGE_ITEM_URI_INDICATOR = 4;
	private static final int USER_PLEDGE_DIR_URI_INDICATOR = 5;
	private static final int USER_PLEDGE_ITEM_URI_INDICATOR = 6;
	private static final int SHARED_PLEDGE_DIR_URI_INDICATOR = 7;
	private static final int SHARED_PLEDGE_ITEM_URI_INDICATOR = 8;

	private static final UriMatcher sURIMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);
	static {
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME + "/",
				USER_CHALLENGE_DIR_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME + "/#",
				USER_CHALLENGE_ITEM_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME + "/",
				RECEIVED_CHALLENGE_DIR_URI_INDICATOR);
		sURIMatcher
				.addURI(AUTHORITY,
						EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME
								+ "/#", RECEIVED_CHALLENGE_ITEM_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME + "/",
				USER_PLEDGE_DIR_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME + "/#",
				USER_PLEDGE_ITEM_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME + "/",
				SHARED_PLEDGE_DIR_URI_INDICATOR);
		sURIMatcher.addURI(AUTHORITY,
				EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME + "/#",
				SHARED_PLEDGE_ITEM_URI_INDICATOR);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = ead.getWritableDatabase();
		Log.w("table name, deleting row", uri.getLastPathSegment());
		int numRows = db.delete(uri.getLastPathSegment(), selection, null);
		// ---if added successfully---
		if (numRows > 0) {
			getContext().getContentResolver().notifyChange(uri, null);
			return numRows;
		}
		throw new SQLException("Failed to delete row from " + uri);
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		if (sURIMatcher.match(uri) == USER_CHALLENGE_DIR_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == USER_CHALLENGE_ITEM_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == RECEIVED_CHALLENGE_DIR_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == RECEIVED_CHALLENGE_ITEM_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == USER_PLEDGE_DIR_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == USER_PLEDGE_ITEM_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == SHARED_PLEDGE_DIR_URI_INDICATOR) {

		} else if (sURIMatcher.match(uri) == SHARED_PLEDGE_ITEM_URI_INDICATOR) {

		}

		SQLiteDatabase db = ead.getWritableDatabase();
		Log.w("table name, inserting row", uri.getLastPathSegment());
		long rowID = db.insert(uri.getLastPathSegment(), null, values);
		
		Log.w("inserted row", ""+ rowID + " and contents: " + values.toString());
		// ---if added successfully---
		if (rowID > 0) {
			Uri _uri = ContentUris.withAppendedId(uri, rowID);
			getContext().getContentResolver().notifyChange(_uri, null);
			return _uri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		ead = new EnergyAppDatabase(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		int uriType = sURIMatcher.match(uri);
		switch (uriType) {
		case USER_CHALLENGE_DIR_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME);
			break;
		case USER_CHALLENGE_ITEM_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME);
			queryBuilder.appendWhere(EnergyAppDatabaseMetadata._ID + "="
					+ uri.getPathSegments().get(1));
			break;
		case RECEIVED_CHALLENGE_DIR_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME);
			break;
		case RECEIVED_CHALLENGE_ITEM_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME);
			queryBuilder.appendWhere(EnergyAppDatabaseMetadata._ID + "="
					+ uri.getPathSegments().get(1));
			break;
		case USER_PLEDGE_DIR_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME);

			break;
		case USER_PLEDGE_ITEM_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME);
			queryBuilder.appendWhere(EnergyAppDatabaseMetadata._ID + "="
					+ uri.getPathSegments().get(1));
			break;
		case SHARED_PLEDGE_DIR_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME);

			break;

		case SHARED_PLEDGE_ITEM_URI_INDICATOR:
			queryBuilder
					.setTables(EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME);
			queryBuilder.appendWhere(EnergyAppDatabaseMetadata._ID + "="
					+ uri.getPathSegments().get(1));
			break;

		default:
			throw new IllegalArgumentException("Unkown URI " + uri);
		}

		Cursor cursor = queryBuilder.query(ead.getReadableDatabase(),
				projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = ead.getWritableDatabase();
		long rowID = 0;
		if (sURIMatcher.match(uri) == USER_CHALLENGE_ITEM_URI_INDICATOR) {
			rowID = db.update(
					EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME,
					values, selection, null);
		} else if (sURIMatcher.match(uri) == RECEIVED_CHALLENGE_ITEM_URI_INDICATOR) {
			rowID = db.update(
					EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME,
					values, selection, null);
		} else if (sURIMatcher.match(uri) == USER_PLEDGE_ITEM_URI_INDICATOR) {
			rowID = db.update(
					EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME, values,
					selection, null);
		} else if (sURIMatcher.match(uri) == SHARED_PLEDGE_ITEM_URI_INDICATOR) {
			rowID = db.update(
					EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME,
					values, selection, null);
		}

		// ---if added successfully---
		if (rowID > 0) {
			Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(_uri, null);
			return 1;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}
}
